package com.bangdream.michelia.view.fragment.main.curriculum;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.entity.CommentBean;
import com.bangdream.michelia.entity.RxEventBean;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.entity.CoursesTypeBean;
import com.bangdream.michelia.entity.courses.TeacherListBean;
import com.bangdream.michelia.presenter.CoursesPresenter;
import com.bangdream.michelia.utils.L;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.utils.RxBus;
import com.bangdream.michelia.view.activity.curriculum.CurriculumDetails;
import com.bangdream.michelia.view.adapter.CourseItemAdapter;
import com.bangdream.michelia.view.adapter.CurriculumItemAdapter;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * @date on 2018/6/18 14:55
 * @email 909732184@qq.com
 * 课程模块 viewpage中的每页item
 */
public class CurriculumViewPageItem extends LazyLoadFragment<CoursesContract.ICoursesView,CoursesPresenter<CoursesContract.ICoursesView>>
implements CoursesContract.ICoursesView{
    private View view;
    private SmartRefreshLayout outView;								//刷新加载控件
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private CourseItemAdapter adapter;
    private CoursesTypeBean typeData;
    private int page = 1;
    private List<CoursesBean> allData;
    //用于展示搜索结果的临时集合
    private List<CoursesBean> tempData = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.curriculum_item_fragment_layout,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        operateBus();
    }

    @Override
    public CoursesPresenter createPresenter() {
        return new CoursesPresenter();
    }

    @Override
    public void loadData() {
        adapter.setEmptyView(loadingView);
        load();
    }

    private void load(){
        Map paramsMap = new HashMap();
        //第一列为全部 没有分类信息
        if(typeData!=null) {
            paramsMap.put("courseClassifiId", typeData.getId());
        }
        mPresenter.getCoursesListData(page, PublicResources.PageSize,
                CoursesContract.CoursesType.Ordinary,paramsMap,null);
    }

    @SuppressLint("CheckResult")
    public void operateBus() {
        disposable = RxBus.getInstance().toObservable().map(o -> (RxEventBean) o).subscribe(bean -> {
            if (bean.getCode() == 0x02) {

            }
        });
    }


    private void initView() {
        Bundle bundle = getArguments();
        if(bundle!=null){
            typeData = (CoursesTypeBean) bundle.getSerializable("typeData");
        }
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(linearLayoutManager);
        initEmptyView(recyclerView);
        adapter = new CourseItemAdapter(R.layout.curriculum_adapter_item);
        recyclerView.setAdapter(adapter);
        outView = view.findViewById(R.id.sRefreshLayout);
        outView.setEnableLoadmore(false);
        outView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //refreshlayout.finishRefresh(2000);
                page = 1;
                loadData();
            }
        });
        outView.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                //refreshlayout.finishLoadmore(2000);
                page++;
                loadData();
            }
        });

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter a, View view, int position) {
                Intent intent = new Intent(getActivity(),CurriculumDetails.class);
                intent.putExtra("id",adapter.getData().get(position).getId());
                intent.putExtra("title",adapter.getData().get(position).getName());
                List<TeacherListBean> teacherList = adapter.getData().get(position).getTeacherList();
                if(teacherList!=null && teacherList.size()>0)
                intent.putExtra("teacherId",adapter.getData().get(position).getTeacherList().get(0).getTeacherId());
                startActivity(intent);

            }
        });
    }

    @Override
    public void setCoursestList(boolean isSuccess, int page, CoursesContract.CoursesType type, List<CoursesBean> data, String stDescribe) {
        outView.finishRefresh();
        outView.finishLoadmore();

        if (isSuccess && data!=null){
            if(page==1) {
                if( data.size()==0){
                    adapter.setNewData(null);
                    adapter.setEmptyView(noDataView);
                }else {
                    adapter.setNewData(data);
                    adapter.notifyDataSetChanged();
                    //显示数据大于20条才允许上拉加载更多
                    if(data.size()>=PublicResources.PageSize){
                        outView.setEnableLoadmore(true);
                    }
                }
            }else {
                adapter.addData(data);
            }
            allData = adapter.getData();
        }else {
            //如果是加载新数据时失败了 则page-1
            if (page>1){
                this.page--;
            }else {
                //还是还原为1防止出现负数
                this.page = 1;
                adapter.setNewData(null);
                adapter.setEmptyView(isSuccess?noDataView:errorView);
                outView.finishRefresh();
            }
        }
    }

    @Override
    public void setCoursesDetails(boolean isSuccess, CoursesBean data, String stDescribe) {

    }

    @Override
    public void setCoursesTypeList(boolean isSuccess, List<CoursesTypeBean> data, String stDescribe) {

    }

    @Override
    public void setCommentList(boolean isSuccess, int page, List<CommentBean> data, String stDescribe) {

    }

    @Override
    public void actionComment(boolean isSuccess, String stDescribe) {

    }


    public void searchList(String stText){
        tempData.clear();
        if(stText!=null&&stText.length()>0){
            if(allData==null)
                return;
            for(int i = 0;i<allData.size();i++){
                if(allData.get(i).getName().contains(stText)){
                    tempData.add(allData.get(i));
                }
            }
            adapter.setNewData(tempData);
        }else {
            adapter.setNewData(allData);
        }
        adapter.notifyDataSetChanged();
    }
}
